package org.cncitrus.data;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cncitrus.data.common.Params;
import org.cncitrus.data.db.ChinaAreaConn;
import org.cncitrus.data.db.ChinaCBSYConn;
import org.cncitrus.data.db.ChinaExportConn;
import org.cncitrus.data.db.ChinaImportConn;
import org.cncitrus.data.db.ChinaTadeConn;
import org.cncitrus.data.db.ChinaYieldConn;
import org.cncitrus.data.db.UserConn;
import org.cncitrus.data.db.WorldTradeConn;
import org.cncitrus.data.db.WorldYieldAreaConn;

/**
 * Servlet implementation class ClientReq
 */
public class ClientReq extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClientReq() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
//		String chrset = request.getCharacterEncoding() ;
//		System.out.println(chrset+"123123");
		String xmlcontent = "柑橘数据库请求接口";
		
		String atp = request.getParameter("atp");	
		System.out.println(atp);
		if(atp!=null){
			if(atp.equals(Params.SearchChinaYield)){//中国柑橘产量搜索
				try {
					xmlcontent = new ChinaYieldConn().load(request.getParameterValues("year"), request.getParameterValues("pro"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchChinaArea)){//中国柑橘面积搜索
				try {
					xmlcontent = new ChinaAreaConn().load(request.getParameterValues("year"), request.getParameterValues("pro"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchChinaExport)){//中国柑橘出口搜索
				try {
					xmlcontent = new ChinaExportConn().load(request.getParameterValues("year"), request.getParameterValues("code"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchChinaImport)){//中国柑橘进口搜索
				try {
					xmlcontent = new ChinaImportConn().load(request.getParameterValues("year"), request.getParameterValues("code"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchChinaTrade)){//中国柑橘海关贸易数据搜索
				try {
					xmlcontent = new ChinaTadeConn().load(request.getParameterValues("year"), request.getParameterValues("code"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchChinaCBSY)){//中国柑橘成本收益搜索
				try {
					xmlcontent = new ChinaCBSYConn().load(request.getParameterValues("spec"), request.getParameterValues("year"), request.getParameterValues("pro"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchWorldYieldArea)){//国际柑橘产量与面积搜索
				try {
					xmlcontent = new WorldYieldAreaConn().load(request.getParameterValues("year"), request.getParameterValues("country"), request.getParameterValues("spec"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.SearchWorldTrade)){//国际柑橘贸易数据搜索
				try {
					xmlcontent = new WorldTradeConn().load(request.getParameterValues("ec"), request.getParameterValues("ic"), request.getParameterValues("year"), request.getParameterValues("code"));
				} catch (Exception e) {
					xmlcontent = e.toString();
					e.printStackTrace();
				}
				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.AddChinaArea)){//中国柑橘面积添加
				
				try {
					xmlcontent = new ChinaAreaConn().insert(request.getParameterValues("year"), request.getParameterValues("pro"), request.getParameterValues("area"));
//					xmlcontent = "中国柑橘面积添加数据成功";
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = "中国柑橘面积添加数据失败\n"+e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
				
//				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
//				response.getWriter().print(new String("中国柑橘面积添加数据成功".getBytes(),"UTF-8"));
//				response.getWriter().close();
			}else if(atp.equals(Params.AddChinaYield)){//中国柑橘产量添加
				try {
					xmlcontent = new ChinaYieldConn().insert(request.getParameterValues("year"), request.getParameterValues("pro"), request.getParameterValues("t1"), request.getParameterValues("t2"), request.getParameterValues("t3"), request.getParameterValues("t4"), request.getParameterValues("t5"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.sendRedirect("citrus_admin\\add_err.html");
					xmlcontent = e.toString();
				}
//				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.AddChinaExport)){//中国柑橘出口添加
				try {
					xmlcontent = new ChinaExportConn().insert(request.getParameterValues("year"), request.getParameterValues("code"), request.getParameterValues("country"), request.getParameterValues("money"), request.getParameterValues("total"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
			}else if(atp.equals(Params.AddChinaImport)){//中国柑橘进口添加
				try {
					xmlcontent = new ChinaImportConn().insert(request.getParameterValues("year"), request.getParameterValues("code"), request.getParameterValues("country"), request.getParameterValues("money"), request.getParameterValues("total"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
//				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.AddChinaTrade)){//中国柑橘海关贸易数据添加
				try {
					xmlcontent = new ChinaTadeConn().insert(request.getParameterValues("year"), request.getParameterValues("code"), request.getParameterValues("i_total"), request.getParameterValues("i_money"), request.getParameterValues("e_total"), request.getParameterValues("e_money"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
//				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.AddChinaCBSY)){//中国柑橘成本收益数据添加
				try {
					xmlcontent = new ChinaCBSYConn().insert(request.getParameterValues("year"), request.getParameterValues("class"), request.getParameterValues("spec"), request.getParameterValues("con"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
//				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.AddWorldYieldArea)){//国际柑橘产量与面积搜索
				try {
					xmlcontent = new WorldYieldAreaConn().insert(request.getParameterValues("year"), request.getParameterValues("country"), request.getParameterValues("spec"), request.getParameterValues("area"), request.getParameterValues("products"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
//				response.getOutputStream().write(xmlcontent.getBytes("UTF-8"));
			}else if(atp.equals(Params.AddWorldTrade)){//国际柑橘贸易数据添加
				try {
					xmlcontent = new WorldTradeConn().insert(request.getParameterValues("year"), request.getParameterValues("code"), request.getParameterValues("ec"), request.getParameterValues("ic"), request.getParameterValues("total"), request.getParameterValues("money"));
					response.sendRedirect("citrus_admin\\add_sucess.html");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					xmlcontent = e.toString();
					response.sendRedirect("citrus_admin\\add_err.html");
				}
			}else if(atp.equals(Params.Uservaliate)){
				boolean login;
				try {
					login = new UserConn().login(request.getParameter("usr"), request.getParameter("psw"));
				} catch (SQLException e) {
					login = false;
					e.printStackTrace();
				}
				if(login){
					HttpSession session  = request.getSession();
					session.setAttribute("username", request.getParameter("usr"));				
					response.sendRedirect("citrus_admin/index.jsp");				
				}else{
					HttpSession session  = request.getSession();
					session.setAttribute("username", null);		
					response.sendRedirect("citrus_admin/login.html");
				}
				
			}else if(atp.equals(Params.AddUser)){
				boolean b;
				b = new UserConn().insert(request.getParameter("usr"), request.getParameter("psw"));
				if(b){
					response.sendRedirect("citrus_admin\\add_sucess.html");
				}else{
					response.sendRedirect("citrus_admin\\add_err.html");
				}
			}else if(atp.equals(Params.ModifyPsw)){
				boolean b;
				b = new UserConn().update(request.getParameter("usr"),request.getParameter("oldpsw"),request.getParameter("newpsw"));
				if(b){
					response.sendRedirect("citrus_admin\\add_sucess.html");
				}else{
					response.sendRedirect("citrus_admin\\add_err.html");
				}
			}else if(atp.equals(Params.DeleteUser)){
				System.out.println(atp);
				boolean b;
				b = new UserConn().delete(request.getParameter("uid"));
				if(b){
					response.sendRedirect("citrus_admin\\add_sucess.html");
				}else{
					response.sendRedirect("citrus_admin\\add_err.html");
				}
			}else if(atp.equals(Params.VIPUser)){				
				if(String.valueOf(request.getParameter("token")).equals("0xfefdfcfbr34342783978a")){
//					response.sendRedirect("");
					RequestDispatcher dis = request.getRequestDispatcher("");
					dis.forward(request	, response);
				}else{
					response.sendRedirect("http://www.cncitrus.com/");
				}
			}else{
				response.sendRedirect("http://www.cncitrus.com/");
			}
		}else{
			response.sendRedirect("http://www.cncitrus.com/");
		}
		
	}

}
